package cn.exrick.xboot.modules.transport.controller;

import cn.exrick.xboot.common.utils.BeanConverterUtil;
import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.modules.transport.entity.BigCustomers;
import cn.exrick.xboot.modules.transport.vo.BigCustomersVo;
import cn.exrick.xboot.modules.transport.param.QueryBigCustomersParam;
import cn.exrick.xboot.modules.transport.param.EditBigCustomersParam;
import cn.exrick.xboot.modules.transport.service.BigCustomersService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author gj
 */
@Slf4j
@RestController
@Api(tags = "大客户公司基本信息管理接口")
@RequestMapping("/xboot/bigCustomers")
@Transactional
public class BigCustomersController {

    @Autowired
    private BigCustomersService bigCustomersService;

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<BigCustomersVo> get(@PathVariable Long id) {

        BigCustomersVo bigCustomersvo = BeanConverterUtil.convertBean(bigCustomersService.getById(id), BigCustomersVo.class);
        return new ResultUtil<BigCustomersVo>().setData(bigCustomersvo);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "编辑或更新数据")
    public Result<BigCustomersVo> saveOrUpdate(@RequestBody EditBigCustomersParam editBigCustomersParam) {

        if (bigCustomersService.saveOrUpdate(BeanConverterUtil.convertBean(editBigCustomersParam, BigCustomers.class))) {
            return new ResultUtil<BigCustomersVo>().setData(BeanConverterUtil.convertBean(editBigCustomersParam, BigCustomersVo.class));
        }
        return new ResultUtil<BigCustomersVo>().setErrorMsg("操作失败");
    }

    @RequestMapping(value = "/delByIds", method = RequestMethod.POST)
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delAllByIds(@RequestBody Long[] ids) {

        for (Long id : ids) {
            bigCustomersService.removeBigCustomersById(id);
        }
        return ResultUtil.success("批量通过id删除数据成功");
    }

    @RequestMapping(value = "/getByCondition", method = RequestMethod.GET)
    @ApiOperation(value = "多条件分页获取")
    public Result<IPage<BigCustomersVo>> getByCondition(QueryBigCustomersParam param,
                                                        PageVo page){

        QueryWrapper<BigCustomers> qw = new QueryWrapper<>();
        // 大客户公司名称模糊搜素
        qw.like(StrUtil.isNotBlank(param.getCustomerName()), "company_name", param.getCustomerName());
        //按创建时间倒序
        qw.orderByDesc("create_time");
        IPage<BigCustomersVo> data = BeanConverterUtil.convertPage(bigCustomersService.page(PageUtil.initMpPage(page), qw), BigCustomersVo.class);
        return new ResultUtil<IPage<BigCustomersVo>>().setData(data);
    }
}
